package com.cqupt.slewing

import android.content.Context
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.Headers
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody


class MyRepository(context: Context) {
    private val shp = context.getSharedPreferences("MyData", Context.MODE_PRIVATE)

    companion object {
        private val JSON: MediaType = "application/json; charset=utf-8".toMediaType()
        const val HOST = "http:///124.221.144.30/"
    }

    suspend fun postData(urlString: String, json: String): Boolean {
        var isSuccessful = false
        val client = OkHttpClient()
        val body = json.toRequestBody(JSON)
        val request = Request.Builder()
            .url(HOST + urlString)
            .addHeader("cookie", shp.getString("session", "")!!)
            .post(body)
            .build()
        withContext(Dispatchers.IO) {
            val response = client.newCall(request).execute()
            Log.d("myTAG", "postData: ${response.body?.string()}")
            if (response.code == 200) {
                isSuccessful = true
            }
        }
        return isSuccessful
    }

    suspend fun getData(urlString: String): String {
        var data = ""
        val client = OkHttpClient()
        val request: Request = Request.Builder()
            .url(HOST + urlString)
            .addHeader("cookie", shp.getString("session", "")!!)
            .build()

        withContext(Dispatchers.IO) {
            var response = client.newCall(request).execute()
            val tempResponse = response.body?.string()
            Log.d("myTAG", "getData: $tempResponse")
            data = tempResponse!!
        }
        return data
    }

    suspend fun login(json: String): String {
        var session: String = ""
        val client = OkHttpClient()
        val body = json.toRequestBody(JSON)
        val request = Request.Builder()
            .url(HOST + "user/login")
            .post(body)
            .build()
        withContext(Dispatchers.IO) {
            val response = client.newCall(request).execute()
            Log.d("myTAG", "postData: ${response.body?.string()}")
            val headers: Headers = response.headers
            val cookies = headers.values("Set-Cookie")
            if (cookies.isNotEmpty()) {
                session = cookies[0]
            }
        }
        Log.d("myTAG", "session: $session")
//                val rememberToken: String = cookies[1]
//                Log.d("myTAG", "remember-token: $rememberToken")
        return session
    }

    suspend fun logout(): Boolean {
        var isSuccessful = false
        val client = OkHttpClient()
        val request: Request = Request.Builder()
            .url(HOST + "user/logout")
            .addHeader("cookie", shp.getString("session", "")!!)
            .build()

        withContext(Dispatchers.IO) {
            val response = client.newCall(request).execute()
            Log.d("myTAG", "logout: ${response.body?.string()}")
            if (response.code == 200) {
                isSuccessful = true
            }
        }
        return isSuccessful
    }
}